﻿using ChurchDiary.Common;
using ChurchDiary.DAL.Contract;
using ChurchDiary.DAL.DAL;
using ChurchDiary.TableEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChurchDiary.DAL.Methods
{
    public class DAL_InitialSettings : DataAccess, IInitialSettings
    {
        
        public EntityFullIntialSettings GetUserIntialSettings(Guid Userid)
        {
            EntityFullIntialSettings FullIntialSettings = new EntityFullIntialSettings();
            try
            {
                List<EntityInitialSettings>  InitialSettings =  new List<EntityInitialSettings> ();
                List<EnitityUserSettings> UserSettings = new List<EnitityUserSettings>();
                using (DbCommand dbcommand = SQLServer.GetStoredProcCommand("[dbo].[GET_InitialSettings]"))
                {
                    SQLServer.AddInParameter(dbcommand, "@Userid", DbType.Guid, Userid);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {

                        using (IDataReader OReader = SQLServer.ExecuteReader(dbcommand))
                        {
                            if (null == OReader)
                            {
                                return FullIntialSettings;
                            }
                            while (OReader.Read())
                            {
                                EntityInitialSettings objInitialSettings = new EntityInitialSettings();
                                objInitialSettings.SettingsId = (Guid)OReader["SettingsId"];
                                objInitialSettings.SettingsName = OReader["SettingsName"].ToString();
                                objInitialSettings.SettingsStatus =(int)OReader["SettingsStatus"];
                                objInitialSettings.UserTypeId = (Guid)OReader["UserTypeId"];
                                objInitialSettings.PopupName = OReader["PopupName"].ToString();
                                objInitialSettings.OrderNo = (int)OReader["OrderNo"];
                                InitialSettings.Add(objInitialSettings);
                            }
                            FullIntialSettings.InitialSettings = InitialSettings;
                            OReader.NextResult();
                            while (OReader.Read())
                            {
                                EnitityUserSettings objUserSettings = new EnitityUserSettings();
                                objUserSettings.SettingsId = (Guid)OReader["SettingsId"];
                                objUserSettings.SettingsStatus = (int)OReader["SettingsStatus"];
                                objUserSettings.Userid = (Guid)OReader["Userid"];
                                objUserSettings.UserSettingsId = (Guid)OReader["IsActive"];
                                UserSettings.Add(objUserSettings);
                            }
                            FullIntialSettings.UserSettings = UserSettings;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return FullIntialSettings;
        }
        public EntityFullIntialSettings GetUserSettings(Guid Userid)
        {
            EntityFullIntialSettings FullIntialSettings = new EntityFullIntialSettings();
            try
            {
                List<EntityInitialSettings> InitialSettings = new List<EntityInitialSettings>();
                List<EnitityUserSettings> UserSettings = new List<EnitityUserSettings>();
                using (DbCommand dbcommand = SQLServer.GetStoredProcCommand("[dbo].[GET_InitialSettingByUser]"))
                {
                    SQLServer.AddInParameter(dbcommand, "@Userid", DbType.Guid, Userid);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {

                        using (IDataReader OReader = SQLServer.ExecuteReader(dbcommand))
                        {
                            if (null == OReader)
                            {
                                return FullIntialSettings;
                            }
                            while (OReader.Read())
                            {
                                EntityInitialSettings objInitialSettings = new EntityInitialSettings();
                                objInitialSettings.SettingsId = (Guid)OReader["SettingsId"];
                                objInitialSettings.SettingsName = OReader["SettingsName"].ToString();
                                objInitialSettings.SettingsStatus = (int)OReader["SettingsStatus"];
                                objInitialSettings.UserTypeId = (Guid)OReader["UserTypeId"];
                                objInitialSettings.PopupName = OReader["PopupName"].ToString();
                                objInitialSettings.OrderNo = (int)OReader["OrderNo"];
                                InitialSettings.Add(objInitialSettings);
                            }
                            FullIntialSettings.InitialSettings = InitialSettings;
                            while (OReader.Read())
                            {
                                EnitityUserSettings objUserSettings = new EnitityUserSettings();
                                objUserSettings.SettingsId = (Guid)OReader["SettingsId"];
                                objUserSettings.SettingsStatus = (int)OReader["SettingsStatus"];
                                objUserSettings.Userid = (Guid)OReader["Userid"];
                                objUserSettings.UserSettingsId = (Guid)OReader["IsActive"];
                                UserSettings.Add(objUserSettings);
                            }
                            FullIntialSettings.UserSettings = UserSettings;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return FullIntialSettings;
        }
        public string InsertUserSettings(string popupName, int Status)
        {
            string IResult = "SME";
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[I_UserSettings]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@Userid", DbType.Guid, Current.Userid);
                    SQLServer.AddInParameter(dbCommand, "@Status", DbType.Int32, Status);
                    SQLServer.AddInParameter(dbCommand, "@PopUpName", DbType.String, popupName);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        IResult = (string)SQLServer.ExecuteScalar(dbCommand);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return IResult;
        }
        public string ChangePassword(string OldPassword, string NewPassword, Guid Userid)
        {
            string IResult = "SME";
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[U_ChangePasword]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@Userid", DbType.Guid, Current.Userid);
                    SQLServer.AddInParameter(dbCommand, "@NewPassword", DbType.String, NewPassword);
                    SQLServer.AddInParameter(dbCommand, "@UserOldPassword", DbType.String, OldPassword);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        IResult = (string)SQLServer.ExecuteScalar(dbCommand);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return IResult;
        }
        public string ChangeProfile(string imagename)
        {
            string IResult = "SME";
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[U_ChangeProfile]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@UserId", DbType.Guid, Current.Userid);
                    SQLServer.AddInParameter(dbCommand, "@ImageName", DbType.String, imagename);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        IResult = (string)SQLServer.ExecuteScalar(dbCommand);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return IResult;
        }
        public string UpdateControllerAndAction(EntityControllerActions ControllerActions)
        {
            string IResult = "SME";
            var guid = new Guid("00000000-0000-0000-0000-000000000000");
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[IDU_ControllerActions]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@Mode", DbType.String, GETMODE.ONE);
                    SQLServer.AddInParameter(dbCommand, "@ControllerActionId", DbType.Int32, 0);
                    SQLServer.AddInParameter(dbCommand, "@ActionId", DbType.Guid, guid);
                    SQLServer.AddInParameter(dbCommand, "@ControllerName", DbType.String, ControllerActions.ControllerName);
                    SQLServer.AddInParameter(dbCommand, "@ActionName", DbType.String, ControllerActions.ActionName);
                    SQLServer.AddInParameter(dbCommand, "@ActionDesc", DbType.String, ControllerActions.ActionDescription);
                    SQLServer.AddInParameter(dbCommand, "@Template", DbType.String, ControllerActions.NotifcationTemplate);
                    SQLServer.AddInParameter(dbCommand, "@by", DbType.Guid, Current.Userid);
                    SQLServer.AddInParameter(dbCommand, "@On", DbType.DateTime, DateTime.Now);
                    SQLServer.AddInParameter(dbCommand, "@Priorty", DbType.Int32, ControllerActions.Priorty);
                    SQLServer.AddInParameter(dbCommand, "@Status", DbType.Int32, ControllerActions.status);
                    SQLServer.AddInParameter(dbCommand, "@EmailStatus", DbType.Boolean, ControllerActions.EmailStatus);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        IResult = (string)SQLServer.ExecuteScalar(dbCommand);
                        
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return IResult;
        }
        public List<EntityControllerActions> GetControllerAction()
        {
            List<EntityControllerActions> ControllerActions = new List<EntityControllerActions>();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_ControllerActions]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@Mode", DbType.String, GETMODE.LIST);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return ControllerActions;
                            }
                            while (OReader.Read())
                            {
                                EntityControllerActions ControllerAction = new EntityControllerActions();
                                ControllerAction.ControllerActionsId = (Int32)OReader["ControllerActionsId"];
                                ControllerAction.ActionId = (Guid)OReader["ActionId"];
                                ControllerAction.ControllerName = OReader["ControllerName"].ToString();
                                ControllerAction.ActionName = OReader["ActionName"].ToString();
                                ControllerAction.ActionDescription = OReader["ActionDescription"].ToString();
                                ControllerAction.NotifcationTemplate = OReader["NotifcationTemplate"].ToString();
                                ControllerAction.ActivatedTime = (DateTime)OReader["ActivatedTime"];
                                ControllerAction.Priorty = (Int32)OReader["Priorty"];
                                ControllerAction.status = (Int32)OReader["status"];
                                ControllerAction.IsDeleted = (bool)OReader["IsDeleted"];
                                ControllerActions.Add(ControllerAction);
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return ControllerActions;
        }
        public EntityControllerActions GetControllerAction(Guid ActionId)
        {
            EntityControllerActions ControllerAction = new EntityControllerActions();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_ControllerActions]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@Mode", DbType.String, GETMODE.ONE);
                    SQLServer.AddInParameter(dbCommand, "@ActionId", DbType.Guid, ActionId);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return ControllerAction;
                            }
                            while (OReader.Read())
                            {
                                ControllerAction.ControllerActionsId = (Int32)OReader["ControllerActionsId"];
                                ControllerAction.ActionId = (Guid)OReader["ActionId"];
                                ControllerAction.ControllerName = OReader["ControllerName"].ToString();
                                ControllerAction.ActionName = OReader["ActionName"].ToString();
                                ControllerAction.ActionDescription = OReader["ActionDescription"].ToString();
                                ControllerAction.NotifcationTemplate = OReader["NotifcationTemplate"].ToString();
                                ControllerAction.ActivatedTime = (DateTime)OReader["ActivatedTime"];
                                ControllerAction.Priorty = (Int32)OReader["Priorty"];
                                ControllerAction.status = (Int32)OReader["status"];
                                ControllerAction.IsDeleted = (bool)OReader["IsDeleted"];
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return ControllerAction;
        }
        public string UpdateControllerDetails(EntityControllerActions ControllerActions)
        {
            string IResult = "SME";
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[IDU_ControllerActions]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@Mode", DbType.String, eMode.INS);
                    SQLServer.AddInParameter(dbCommand, "@ControllerActionId", DbType.Int32, ControllerActions.ControllerActionsId);
                    SQLServer.AddInParameter(dbCommand, "@ActionId", DbType.Guid, ControllerActions.ActionId);
                    SQLServer.AddInParameter(dbCommand, "@ControllerName", DbType.String, ControllerActions.ControllerName);
                    SQLServer.AddInParameter(dbCommand, "@ActionName", DbType.String, ControllerActions.ActionName);
                    SQLServer.AddInParameter(dbCommand, "@ActionDesc", DbType.String, ControllerActions.ActionDescription);
                    SQLServer.AddInParameter(dbCommand, "@Template", DbType.String, ControllerActions.NotifcationTemplate);
                    SQLServer.AddInParameter(dbCommand, "@by", DbType.Guid, Current.Userid);
                    SQLServer.AddInParameter(dbCommand, "@On", DbType.DateTime, DateTime.Now);
                    SQLServer.AddInParameter(dbCommand, "@Priorty", DbType.Int32, ControllerActions.Priorty);
                    SQLServer.AddInParameter(dbCommand, "@Status", DbType.Int32, ControllerActions.status);
                    SQLServer.AddInParameter(dbCommand, "@EmailStatus", DbType.Boolean, ControllerActions.EmailStatus);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        IResult = (string)SQLServer.ExecuteScalar(dbCommand);

                    }
                }
            }
            catch (Exception ex)
            {
            }
            return IResult;
        }
        public string InsertNotificationLog(EntityNotification Notification)
        {
            string IResult = "SME";
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[IDU_Notifation]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@NotificationTo", DbType.Guid, Current.Userid);
                    SQLServer.AddInParameter(dbCommand, "@Notificationby", DbType.Guid, Current.Userid);
                    SQLServer.AddInParameter(dbCommand, "@ControllerName", DbType.String, Notification.ControllerName);
                    SQLServer.AddInParameter(dbCommand, "@ActionName", DbType.String, Notification.ActionName);
                    SQLServer.AddInParameter(dbCommand, "@NotificationTime", DbType.DateTime, DateTime.Now);
                    SQLServer.AddInParameter(dbCommand, "@Status", DbType.Int32, 0);
                    SQLServer.AddInParameter(dbCommand, "@Mode", DbType.String, eMode.INS);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        IResult = (string)SQLServer.ExecuteScalar(dbCommand);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return IResult;
        }
        public List<EntityNotification> GetNotifications()
        {
            List<EntityNotification> Notifications = new List<EntityNotification>();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_Notification]"))
                {
                    SQLServer.AddInParameter(dbCommand, "@Mode", DbType.String, GETMODE.LIST);
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return Notifications;
                            }
                            while (OReader.Read())
                            {
                                EntityNotification Notification = new EntityNotification();
                                Notification.NotificationId = (Guid)OReader["NotificationId"];
                                Notification.Notificationby = (Guid)OReader["Notificationby"];
                                Notification.NotificationString = OReader["NotificationString"].ToString();
                                Notification.ControllerName = OReader["ControllerName"].ToString();
                                Notification.ActionName = OReader["ActionName"].ToString();
                                Notification.status = (Int32)OReader["status"];
                                Notification.time = OReader["time"].ToString();
                                Notification.Name = OReader["Name"].ToString();
                                Notification.Image = OReader["UserPicture"].ToString();
                                Notification.ControllerActions = new EntityControllerActions();
                                Notification.ControllerActions.Priorty = (Int32)OReader["Priorty"];
                                Notifications.Add(Notification);
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return Notifications;
        }
    }
}
